package remin;

import java.awt.Color;
import java.util.Calendar;
import javax.swing.*;

//implements runnable - treba ak chcem thread
public class Clock implements Runnable{
	
	//go premenna
	Thread go;
	String systime,mytime;
	int oldiw = -1;
	RefUdal rud = new RefUdal();
	
	
	//ak je go null tato metoda vytvori nove go - konstruktor threadu a spusti ho
	public void goo(){
	if(go == null){
		go = new Thread(this);
		go.start();
	}
	}
	
	public String getSystime(){
		
		Calendar now = Calendar.getInstance();
        int hour = now.get(Calendar.HOUR_OF_DAY);
        int minute = now.get(Calendar.MINUTE);
        int month = now.get(Calendar.MONTH) + 1;
        int day = now.get(Calendar.DAY_OF_MONTH);
        int year = now.get(Calendar.YEAR);
        
        systime = hour + ":" + minute + " " + day + "." + month + "." + year;
        return systime;
	}
	
	//thread musi obsahovat funkciu run,kde su instrukcie co sa ma robit
	public void run(){
		//skusit ref alebo nejak odstranit
		
		while(true){
      // get current time and date
            try{getSystime();
            rud.refuda();
                   
            
           //display the date and year
          //System.out.println(systime);
                    
            if(systime.equals(OutDate.mytime)&& oldiw != rud.iw){
            	
              oldiw = rud.iw;	
              Upoz.getInstance();
//              System.out.println("zhoda casov");
//        	  System.out.println(rud.iw);
        	  Upoz.getInstance().uddl.setText(RemGui.tabulka.getValueAt(rud.iw, 2).toString());
        	  Upoz.getInstance().priro.setText(RemGui.tabulka.getValueAt(rud.iw, 3).toString());
        	  Upoz.getInstance().toFront();
    	    
        	  
            }
            else {
            	Upoz.setInstance();
            }
          }catch(Exception e){
          	System.out.println("Nie je z coho citat");
          }

         //uspanie threadu vnutri while cyklu
         try{
	        Thread.sleep(5000);
            }catch(Exception e){
	        System.out.println("problem");
            }
		}
       }
}
